/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.icanci.loopstack.damc.admin.dal.mongo.mongo;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import cn.icanci.loopstack.damc.admin.dal.common.PageList;
import cn.icanci.loopstack.damc.admin.dal.mongo.daointerface.AppDAO;
import cn.icanci.loopstack.damc.admin.dal.mongo.dateobject.AppDO;

/**
 * @author icanci
 * @since 1.0 Created in 2025/05/16 22:45
 */
@Service("appDAO")
public class MongoAppDAO extends AbstractBaseDAO<AppDO> implements AppDAO {
    @Override
    public void insert(AppDO app) {
        super.insert(app);
        mongoTemplate.insert(app, COLLECTION_NAME);
    }

    @Override
    public void update(AppDO app) {
        super.update(app);
        mongoTemplate.save(app, COLLECTION_NAME);
    }

    /**
     * 查询文档所有记录
     *
     * @return 返回查询的结果
     */
    @Override
    public List<AppDO> queryAll() {
        Criteria criteria = Criteria.where(AppColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.find(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    /**
     * 查询文档所有记录
     *
     * @param appDO    请求参数
     * @param pageNum  pageNum
     * @param pageSize pageSize
     * @return 返回查询的结果
     */
    @Override
    public PageList<AppDO> pageQuery(AppDO appDO, int pageNum, int pageSize) {
        Criteria criteria = Criteria.where(AppColumn.ENV).is(DEFAULT_ENV);
        // 查询参数
        if (StringUtils.isNotBlank(appDO.getAppName())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(AppColumn.APP_NAME).regex("^.*" + appDO.getAppName() + ".*$", "i");
        }
        if (StringUtils.isNotBlank(appDO.getAppCode())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(AppColumn.APP_CODE).regex("^.*" + appDO.getAppCode() + ".*$", "i");
        }
        Query query = new Query(criteria);
        query.with(Sort.by(Sort.Direction.DESC, AppColumn.CREATE_TIME));
        return pageQuery(query, COLLECTION_CLASS, pageSize, pageNum, COLLECTION_NAME);
    }

    /**
     * 根据 _id 查询一条信息
     *
     * @param _id _id
     * @return 返回查询的结果
     */
    @Override
    public AppDO queryOneById(String _id) {
        Criteria criteria = Criteria.where(AppColumn._ID).is(_id);
        criteria.and(AppColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public AppDO queryByAppName(String appName) {
        Criteria criteria = Criteria.where(AppColumn.APP_NAME).is(appName);
        criteria.and(AppColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public AppDO queryByApCode(String appCode) {
        Criteria criteria = Criteria.where(AppColumn.APP_CODE).is(appCode);
        criteria.and(AppColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public Class<? extends BaseColumn> getBaseColumn() {
        return AppColumn.class;
    }
}
